package com.hdc.hdc.warehouseManage.listener;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import com.hdc.hdc.warehouseManage.entity.vo.MsgBean;

@Service("eventListenner")
public class EventListenner {
	
	
	private static final Logger log = LoggerFactory.getLogger(EventListenner.class);

	
	@Resource(name = "clientCache")
    private SocketIOClientCache clientCache;
 
    @Resource(name = "socketIOResponse")
    private SocketIOResponse socketIOResponse;
     
    @OnConnect
    public void onConnect(SocketIOClient client) {
    	log.info("建立连接");
    }
 
    @OnEvent("OnMSG")
    public void onSync(SocketIOClient client, MsgBean bean) {
        log.info("收到消息-from: %s to:%s\n", bean.getFrom(), bean.getTo());
        clientCache.addClient(client, bean);
        SocketIOClient ioClients = clientCache.getClient(bean.getTo());
        log.info("clientCache");
        if (ioClients == null) {
            log.info("你发送消息的用户不在线");
            return;
        }
        socketIOResponse.sendEvent(ioClients,bean);
    }
 
    @OnDisconnect
    public void onDisconnect(SocketIOClient client) {
        log.info("关闭连接");
    }

}
